package com.zhou.plugin.packagejars.pack.impl;

import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.actionSystem.LangDataKeys;
import com.intellij.openapi.compiler.CompileContext;
import com.intellij.openapi.compiler.CompileStatusNotification;
import com.intellij.openapi.compiler.ex.CompilerPathsEx;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.JavaDirectoryService;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiManager;
import com.intellij.psi.PsiPackage;
import com.zhou.plugin.packagejars.message.Messages;
import com.zhou.plugin.packagejars.pack.Packager;
import com.zhou.plugin.packagejars.util.Util;

import java.io.BufferedReader;
import java.io.InputStreamReader;

/**
 * Created by Administrator on 2016/7/8.
 */

public class AllPacker implements Packager, CompileStatusNotification {
	private DataContext dataContext;
	private String exportPath;
	private String exportJarName;

	public AllPacker(DataContext dataContext, String exportPath, String exportJarName) {
		this.dataContext = dataContext;
		this.exportPath = exportPath;
		this.exportJarName = exportJarName;
	}

	public void pack() {
		Project project = (Project) CommonDataKeys.PROJECT.getData(this.dataContext);
		Module module = (Module) LangDataKeys.MODULE.getData(this.dataContext);
		Messages.clear(project);
		VirtualFile[] virtualFiles = (VirtualFile[]) CommonDataKeys.VIRTUAL_FILE_ARRAY.getData(this.dataContext);
		String outPutPath = CompilerPathsEx.getModuleOutputPath(module, false);
		String jdkPath = Util.getJDKPath(project);
		StringBuilder command = new StringBuilder(jdkPath);
		command.append("jar");
		command.append(" cvf ");
		command.append(this.exportPath);
		command.append("/");
		command.append(this.exportJarName);
		VirtualFile[] process = virtualFiles;
		int e = virtualFiles.length;

		for (int str = 0; str < e; ++str) {
			VirtualFile virtualFile = process[str];
			PsiDirectory psiDirectory = PsiManager.getInstance(project).findDirectory(virtualFile);
			if (psiDirectory != null) {
				command.append(" -C ");
				command.append(outPutPath);
				command.append(" ");
				PsiPackage psiPackage = JavaDirectoryService.getInstance().getPackage(psiDirectory);
				command.append(psiPackage.getQualifiedName().replaceAll("\\.", "/"));
				command.append("/");
			}
		}

		Messages.info(project, command.toString());

		try {
			Process var14 = Runtime.getRuntime().exec(command.toString());
			BufferedReader var15 = new BufferedReader(new InputStreamReader(var14.getInputStream()));

			String var16;
			while ((var16 = var15.readLine()) != null) {
				Messages.info(project, var16);
			}
		} catch (Exception var13) {
			var13.printStackTrace();
		}

	}

	public void finished(boolean b, int error, int i1, CompileContext compileContext) {
		if (error == 0) {
			this.pack();
		} else {
			Project project = (Project) CommonDataKeys.PROJECT.getData(this.dataContext);
			Messages.info(project, "compile error");
		}

	}
}
